<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-carousel trigger="click" :autoplay="false" arrow="never" height="430px">
        <el-col :xs="24" :md="12" :lg="8">
          <el-card v-loading="loading" shadow="hover" style="height:420px">
            {{date}} 注册人数
            <div style="display:block;margin-top:5px;margin-bottom:5px;color:#737373;line-height:normal;font-size:12px;">根据搜索条件（统计时间）变化</div>
            <ve-pie :data="registerData" :judge-width="true"></ve-pie>
          </el-card>
        </el-col>
        <el-col :xs="24" :md="12" :lg="8">
          <el-card v-loading="loading" shadow="hover" style="height:420px">
            {{date}} 充值次数
            <div style="display:block;margin-top:5px;margin-bottom:5px;color:#737373;line-height:normal;font-size:12px;">根据搜索条件（统计时间）变化</div>
            <ve-pie :data="chargeData" :judge-width="true"></ve-pie>
          </el-card>
        </el-col>
        <el-col :xs="24" :md="12" :lg="8">
          <el-card v-loading="loading" shadow="hover" style="height:420px">
            {{date}} 充值金额
            <div style="display:block;margin-top:5px;margin-bottom:5px;color:#737373;line-height:normal;font-size:12px;">根据搜索条件（统计时间）变化</div>
            <ve-pie :data="costData" :judge-width="true"></ve-pie>
          </el-card>
        </el-col>
      </el-carousel>
    </el-row>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="日期" prop="date">
        <el-date-picker v-model="queryParams.date" type="date" placeholder="请选择日期"
                        value-format="yyyy-MM-dd" clearable/>
<!--        <el-input v-model="queryParams.date" clearable/>-->
      </el-form-item>
      <el-form-item label="渠道" prop="branch">
        <el-select v-model="queryParams.branch" placeholder="请选择渠道" clearable>
          <el-option v-for="dict in dict.type.sys_branch" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="邀请关系" prop="branch">
        <el-select v-model="queryParams.isInvite" placeholder="请选择是否有邀请关系">
          <el-option v-for="dict in isInviteDict" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="日期" align="center" prop="days"/>
      <el-table-column label="渠道" align="center" prop="branch">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_branch" :value="scope.row.branch"/>
        </template>
      </el-table-column>
      <el-table-column label="注册人数" align="center" prop="regCount"/>
      <el-table-column label="充值次数" align="center" prop="sumCount"/>
      <el-table-column label="充值金额" align="center" prop="sumPrice"/>
      <el-table-column label="首充次数" align="center" prop="firstCount"/>
      <el-table-column label="首充金额" align="center" prop="firstPrice"/>
      <el-table-column label="当日注册并充值的总金额" align="center" prop="regDayPrice"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {
  list
} from "@/api/service/statistics/statistics";

export default {
  name: "statistics",
  dicts: ['sys_branch'],
  data() {
    return {
      open: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 封禁IP和设备记录表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        date: undefined,
        branch: undefined,
        isInvite: 0
      },
      date: undefined,
      isInviteDict : [{label: '有',value: 1},{label: '无',value: 0}],
      registerData: {
        columns: ['agentName', 'countRegister'],
        rows: []
      },
      chargeData: {
        columns: ['agentName', 'countCharge'],
        rows: []
      },
      costData: {
        columns: ['agentName', 'sumCost'],
        rows: []
      },
      agentDict: [
        { value: 101, label: '安卓官网版本' },
        { value: 102, label: '安卓华为版本' },
        { value: 103, label: '安卓vivo版本' },
        { value: 104, label: '安卓oppo版本' },
        { value: 105, label: '安卓小米版本' },
        { value: 106, label: '安卓腾讯版本' },
        { value: 201, label: 'IOS' },
        { value: 301, label: '陌对安卓官网版本' },
        { value: 302, label: '陌对安卓vivo版本' },
        { value: 303, label: '陌对安卓oppo版本' },
        { value: 304, label: '陌对安卓小米版本' },
        { value: 305, label: '陌对安卓阿里版本' },
        { value: 306, label: '陌对安卓腾讯版本' },
        { value: 307, label: '陌对安卓荣耀版本' }],

    };
  },
  created() {
    var date = new Date();
    var year = date.getFullYear();
    var month = this.formatNumber(date.getMonth() + 1);
    var day = this.formatNumber(date.getDate());
    this.queryParams.date = year + '-' + month + '-' + day;
    this.getList();
  },
  methods: {
    formatNumber(num){
      return num < 10 ? '0' + num : num;
    },
    /** 查询封禁IP和设备记录列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.date = this.queryParams.date;
        this.handleChartData(this.list)
      });
    },
    async handleChartData (result) {
      // 图表数据
      const rows = []
      for (const item of result) {
          // if (item.plat === 2) {
          //   const row = rows.find(o => o.agent === item.agent)
          //   row.countRegister += item.countRegister
          //   row.countCharge += item.countCharge
          //   row.sumCost += item.sumCost
          //   continue
          // }
          const row = {}
          row.agentName = this.agentDict.find(o => o.value === item.branch).label
          row.agent = item.branch
          row.countRegister = item.regCount
          row.countCharge = item.sumCount
          row.sumCost = item.sumPrice
          rows.push(row)
      }
      this.registerData.rows = rows
      this.chargeData.rows = rows
      this.costData.rows = rows
      this.loading = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd(){
      this.open = true
    },
    showPlat(value){
      this.show = parseInt(value) === 6;
    }
  }
};
</script>
